//
//  AccountMainTableViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/11.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "AccountTableViewController.h"
#import "AccountListTableViewCell.h"
#import "AccountDepositNextViewController.h"
#import "DigitalExchangeViewController.h"
#import "DigitalTransferViewController.h"
#import "AccountMainViewController.h"

@interface AccountTableViewController ()

@property (nonatomic, assign) NSInteger select;
@property (weak, nonatomic) IBOutlet UIView *scrollBgView;
@property (weak, nonatomic) IBOutlet UIButton *hideButton;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UILabel *address;
@property (nonatomic, strong) UILabel *totalValue;
@property (nonatomic, strong) UILabel *available;

@property (nonatomic, strong) NSMutableArray *displayArray;

@end

@implementation AccountTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *string = [NSString stringWithFormat:@"%@Account", self.ethEntity.metadata.jurisdiction];
    self.title = JFMainLocalizedString(string);
//    self.title = [NSString stringWithFormat:@"%@ Account", [self.ethEntity.metadata.jurisdiction uppercaseString]];
    
    [self.hideButton setTitle:[NSString stringWithFormat:@" %@",JFMainLocalizedString(@"myAssets.hideLowBalances")] forState:UIControlStateNormal];
    
    self.select = -1;
    self.displayArray = [NSMutableArray arrayWithArray:[self.ethEntity.balance sortedArrayUsingComparator:^NSComparisonResult(BalanceListEntity *  _Nonnull obj1, BalanceListEntity *  _Nonnull obj2) {
        return [Currencies().symbolSortArray indexOfObject:obj1.asset_symbol] > [Currencies().symbolSortArray indexOfObject:obj2.asset_symbol];
    }]];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AccountListTableViewCell" bundle:nil] forCellReuseIdentifier:@"AccountListTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    [self addScrollContentView];
}

- (void)addScrollContentView {
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 30, 140)];
    [self.scrollBgView addSubview:self.scrollView];
    
//    for (int i = 0; i < 1; i++) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(self.scrollView.width * 0, 0, self.scrollView.width, 140)];
        [self.scrollView addSubview:view];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.scrollView.bounds];
        [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@banner_bg", self.ethEntity.metadata.jurisdiction]]];
        [view addSubview:imageView];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, self.scrollView.width, 15)];
//        label.text = [NSString stringWithFormat:@"%@ Account Total", [self.ethEntity.metadata.jurisdiction uppercaseString]];
    
    if ([[JFInternationalHelper currentLanguage] isEqualToString:@"en"]) {
        label.text = [NSString stringWithFormat:@"%@ %@", self.title, JFMainLocalizedString(@"myAssets.total")];
    }else {
        label.text = [NSString stringWithFormat:@"%@%@", self.title, JFMainLocalizedString(@"myAssets.total")];
    }
        label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
        label.textAlignment = NSTextAlignmentCenter;
    label.textColor = UIColorFromHex(0xffffff);
        [view addSubview:_address = label];
        
        UILabel *totalValue = [[UILabel alloc]initWithFrame:CGRectMake(0, 56, self.scrollView.width, 25)];
        totalValue.text = [NSString stringWithFormat:@"$ %@", MoneyFormat(self.ethEntity.usdTotalValue)];
        totalValue.font = [UIFont fontWithName:@"PingFangSC-Medium" size:24];
        totalValue.textAlignment = NSTextAlignmentCenter;
    totalValue.textColor = UIColorFromHex(0xffffff);
        [view addSubview:_totalValue = totalValue];
        
        
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 94, self.scrollView.width, 15)];
        label2.text = [NSString stringWithFormat:@"%@: %@", JFMainLocalizedString(@"myAssets.available"), MoneyFormat(self.ethEntity.availableTotalValue)];
        label2.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
        label2.textAlignment = NSTextAlignmentCenter;
    label2.textColor = UIColorFromHex(0xffffff);
        [view addSubview:_available = label2];
//    }
    
    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 120, self.scrollView.width, 10)];
    pageControl.numberOfPages = 3;
    pageControl.currentPage = self.index;
    pageControl.pageIndicatorTintColor = UIColorFromHex(0xB2BEB0);
    pageControl.currentPageIndicatorTintColor = UIColorFromHex(0xFFFFFF);
    [self.scrollBgView addSubview:pageControl];
}


- (IBAction)hideAction:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    
    if (sender.isSelected) {
        [self.displayArray removeAllObjects];
        for (BalanceListEntity *balance in self.ethEntity.balance) {
            if (balance.indicative_usd_equivalent.doubleValue != 0) {
                [self.displayArray addObject:balance];
            }
        }
    }else {
        self.displayArray = [NSMutableArray arrayWithArray:[self.ethEntity.balance sortedArrayUsingComparator:^NSComparisonResult(BalanceListEntity *  _Nonnull obj1, BalanceListEntity *  _Nonnull obj2) {
            return [Currencies().symbolSortArray indexOfObject:obj1.asset_symbol] > [Currencies().symbolSortArray indexOfObject:obj2.asset_symbol];
        }]];
    }
    [self.tableView reloadData];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.displayArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AccountListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountListTableViewCell" forIndexPath:indexPath];
    BalanceListEntity *entity = self.displayArray[indexPath.section];
    cell.entity = entity;
    cell.exchangeButton.enabled = [self.ethEntity.metadata.jurisdiction isEqualToString:@"hk"];
    
    WEAKSELF
    cell.action = ^(FunctionActionType type) {
        switch (type) {
            case FunctionActionTypeDeposit: {
                AccountDepositNextViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"AccountDepositNextViewController"];
                vc.balanceEntity = entity;
                vc.wallet_id = self.ethEntity.wallet_id;
                vc.type = 0;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case FunctionActionTypeExchange: {
                DigitalExchangeViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"DigitalExchangeViewController"];
                vc.walletsId = self.ethEntity.wallet_id;
                vc.asset_address = entity.asset_address;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
                break;
            case FunctionActionTypeTransfer: {
                DigitalTransferViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"DigitalTransferViewController"];
                vc.fromBalance = entity;
                vc.walletsEntity = self.walletsEntity;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
                break;
            case FunctionActionTypeWithdraw: {
                AccountDepositNextViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"AccountDepositNextViewController"];
                vc.balanceEntity = entity;
                vc.wallet_id = self.ethEntity.wallet_id;
                vc.type = 3;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            default:
                break;
        }
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (self.select == indexPath.section) ? 120 : 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 15;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc]init];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.select = indexPath.section;
    [self.tableView reloadData];
}


@end
